/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.sql.*;
import java.util.Hashtable;
import java.util.Vector;
import store.ShopItem;
import store.ShopItem.Unit;

/**
 * ShopDB - The class that we use in order to interpret and grab information
 * from our groceries database. Works!
 * @author Alex
 */
public class ShopDB extends PostgresDB {
    
    private ShopDB instance;
    
    /**
     * ShopDB() - Constructor that sets up our database connection
     * information.
     * 
     * @throws ClassNotFoundException - If we can't find a class.
     * @throws DatabaseTimeoutException - If the database connection times out.
     * @throws ShopDBException - If something in this class messes up.
     * @throws SQLException - If we manage to break something in SQL.
     */
    public ShopDB() throws ClassNotFoundException, DatabaseTimeoutException,
            ShopDBException, SQLException {
        super();
        
        instance = this;
        
        instance.userName = "cst";
        instance.password = "tomacco";
        instance.hostName = "204.174.60.231";
        instance.dbName = "store";
        instance.port = 5432;
    }
    
    /**
     * getListFromTable() - Our actual database interpreter, which grabs all
     * the data fields we need in order to return a proper grocery data table.
     * @return Hashtable containing our data entries.
     * @throws DatabaseTimeoutException - If the database connection times out.
     * @throws ShopDBException - If something in this class screws up.
     * @throws SQLException - If we manage to break something in our SQL.
     */
    public Hashtable<Integer, ShopItem> getListFromTable() throws 
            DatabaseTimeoutException, ShopDBException, SQLException {
        
        Hashtable<Integer, ShopItem> shopItems = new Hashtable<Integer, 
                ShopItem>();
        Statement query;
        ResultSet results;
        
        instance.getConnection();
        
        if (db == null) {
            throw (new ShopDBException("Something's wrong with the database, "
                    + "we have no connection."));
        }
        
        try {
            query = db.createStatement();
        } catch(SQLException e) {
            throw(new ShopDBException("Creating our SQL statement didn't"
                    + " work, reason: ", e));
        }
        
        try {
            results = getResultsWithTimeout("SELECT * FROM items", 10);
        } catch(SQLException e) {
            throw(new ShopDBException("We couldn't make our "
                    + "query work, reason: ", e));
        }
        
        try {
            while(results.next()) {
                shopItems.put(results.getInt(1), new ShopItem(results.getInt(1)
                        , results.getString(2), results.getFloat(3), 
                        Unit.valueOf(results.getString(4))));
            }
        } catch(SQLException e) {
            throw(new ShopDBException("Couldn't parse the results from our "
                    + "query, reason: ", e));
        }
        
        return shopItems;
    }
}
